package com.buba.system.controller;

import com.alibaba.fastjson.JSON;
import com.buba.system.pojo.UserInfo;
import com.buba.system.service.LoginLogService;
import com.buba.system.service.LoginService;
import com.buba.system.service.impl.LoginLogServiceImpl;
import com.buba.system.service.impl.LoginServiceImpl;
import com.buba.util.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 */
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends BaseServlet {
    //声明登录业务层
    LoginService loginService = new LoginServiceImpl();
    LoginLogService loginLogService = new LoginLogServiceImpl();
    @Override
    public void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String flag = request.getParameter("flag");
        if ("VerCode".equals(flag))
            this.VerCode(request,response);
        if ("login".equals(flag))
            this.login(request,response);

    }

    /**
     * 登录
     * @param request
     * @param response
     */
    private void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        //子类调用父类的方法getParam() 获取页面输入的账号，密码
        Map<String, String> param = getParam(request);
        //调用业务层的login方法 将map集合传入login
        UserInfo userInfo = loginService.login(param);
        //获取ip地址
        String ipAddr = getIpAddr(request);

        //用于装返回给浏览器的数据
        Map<String,Object> map = new HashMap();
        if (userInfo==null){
            loginLogService.loginlog(param,ipAddr);
            map.put("login","账号或密码输入错误");
            response.getWriter().write(JSON.toJSONString(map));
        }else {
            //将登录对象保存到一次会话中
            session.setAttribute("login",userInfo);
            //获取登录时间将登录时间保存到一次会话中
            session.setAttribute("tame",param.get("tima"));
            map.put("login","登录成功");
            loginLogService.loginlog(param,userInfo,ipAddr);
            response.getWriter().write(JSON.toJSONString(map));
        }
    }

    /**
     * 判断验证码
     * @param request 请求
     * @param response  响应
     * @throws IOException
     */
    private void VerCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //子类调用父类的方法getParam() 获取页面输入的账号，密码
        Map<String, String> param = getParam(request);
        //获取一次会话中的验证码
        Object code = request.getSession().getAttribute("code");
        //判断验证码
        boolean VerCode = loginService.VerCode(param,code);
        //用于装返回给浏览器的数据
        Map<String,Object> map = new HashMap();
        map.put("VerCode",VerCode);
        //true和false给浏览器
//        response.getWriter().write(VerCode+"");
        response.getWriter().write(JSON.toJSONString(map));
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if ("0:0:0:0:0:0:0:1".equals(ip)) {
            ip = "127.0.0.1";
        }
        if (ip.split(",").length > 1) {
            ip = ip.split(",")[0];
        }
        return ip;
    }
}
